package cn.iocoder.yudao.adminserver.modules.fi.controller.project;

import cn.iocoder.yudao.adminserver.modules.fi.controller.project.vo.*;
import cn.iocoder.yudao.adminserver.modules.fi.convert.project.ProjectConvert;
import cn.iocoder.yudao.adminserver.modules.fi.dal.dataobject.project.ProjectDO;
import cn.iocoder.yudao.adminserver.modules.fi.service.project.ProjectService;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysUserDO;
import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "项目管理")
@RestController
@RequestMapping("/fi/project/")
@Validated
public class ProjectController {

    @Resource
    private ProjectService Service;

    @Resource
    private SysUserService userService;

    @PostMapping("/create")
    @ApiOperation("创建项目管理")
    @PreAuthorize("@ss.hasPermission('fi:project:create')")
    public CommonResult<Long> create(@Valid @RequestBody ProjectCreateReqVO createReqVO) {
        return success(Service.create(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新项目管理")
    @PreAuthorize("@ss.hasPermission('fi:project:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody ProjectUpdateReqVO updateReqVO) {
        Service.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除项目管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fi:project:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        Service.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得项目管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('fi:project:query')")
    public CommonResult<ProjectRespVO> get(@RequestParam("id") Long id) {
        ProjectDO  obj = Service.get(id);
        Collection<Long> userIds = new ArrayList<>();
        CollectionUtils.addIfNotNull(userIds, obj.getAreaManagerUid());
        CollectionUtils.addIfNotNull(userIds, obj.getProjectLeaderUid());
        CollectionUtils.addIfNotNull(userIds, obj.getDirectorUid());
        if(StringUtils.isNotBlank(obj.getCreator())){
            CollectionUtils.addIfNotNull(userIds, Long.parseLong(obj.getCreator()));
        }
        Map<Long, SysUserDO> userMap = this.userService.getUserMap(userIds);
        return success(ProjectConvert.INSTANCE.convert(obj, userMap));
    }

    @GetMapping("/list")
    @ApiOperation("获得项目管理列表")
    @PreAuthorize("@ss.hasPermission('fi:project:query')")
    public CommonResult<List<ProjectRespVO>> getList(@Valid ProjectExportReqVO pageVO) {
        List<ProjectDO> list = Service.getList(pageVO);
        return success(ProjectConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得项目管理分页")
    @PreAuthorize("@ss.hasPermission('fi:project:query')")
    public CommonResult<PageResult<ProjectRespVO>> getPage(@Valid ProjectPageReqVO pageVO) {
        PageResult<ProjectDO> pageResult = Service.getPage(pageVO);
        // 获得拼接需要的数据
        Collection<Long> userIds = CollectionUtils.convertList(pageResult.getList(), ProjectDO::getAreaManagerUid);
        Map<Long, SysUserDO> userMap = this.userService.getUserMap(userIds);
        List<ProjectRespVO> list = new ArrayList<>();
        pageResult.getList().forEach(projectObj -> {
            ProjectRespVO respVO = ProjectConvert.INSTANCE.convert(projectObj, userMap);
            list.add(respVO);
        });
        return success(new PageResult<ProjectRespVO>(list, pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出项目管理 Excel")
    @PreAuthorize("@ss.hasPermission('fi/project::export')")
    @OperateLog(type = EXPORT)
    public void exportExcel(@Valid ProjectExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProjectDO> list = Service.getList(exportReqVO);
        // 导出 Excel
        List<ProjectExcelVO> datas = ProjectConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "项目管理.xls", "数据", ProjectExcelVO.class, datas);
    }

}
